package com.jiayan.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayan.mapper.EbProductCategoryMapper;
import com.jiayan.mapper.EbProductMapper;
import com.jiayan.model.EbProduct;
import com.jiayan.model.EbProductCategory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.List;

@Component
public class SpringInit implements CommandLineRunner{
    @Resource
    private EbProductCategoryMapper ebProductCategoryMapper;

    @Resource
    private EbProductMapper ebProductMapper;

    @Resource
    private ServletContext application;

    @Override
    public  void run(String...args) throws Exception{

        //父类
        List<EbProductCategory> parent = ebProductCategoryMapper.selectList(new QueryWrapper<EbProductCategory>().apply("epc_id = epc_parent_id"));
        //子类
        List<EbProductCategory> son = ebProductCategoryMapper.selectList(new QueryWrapper<EbProductCategory>().apply("epc_id != epc_parent_id"));

        Page<EbProduct> page1 = new Page<>(1, 8);
        Page<EbProduct> page2 = new Page<>(1, 8);
        Page<EbProduct> page3 = new Page<>(1, 8);
        Page<EbProduct> page4 = new Page<>(1, 8);

        Page<EbProduct> fruit = ebProductMapper.selectPage(page1, Wrappers.<EbProduct>lambdaQuery().eq(EbProduct::getEpcId, "631"));
        Page<EbProduct> vegetable = ebProductMapper.selectPage(page2, Wrappers.<EbProduct>lambdaQuery().eq(EbProduct::getEpcId, "542"));
        Page<EbProduct> meat = ebProductMapper.selectPage(page3, Wrappers.<EbProduct>lambdaQuery().eq(EbProduct::getEpcId, "636"));
        Page<EbProduct> grain = ebProductMapper.selectPage(page4, Wrappers.<EbProduct>lambdaQuery().eq(EbProduct::getEpcId, "548"));

        application.setAttribute("bigList",parent);
        application.setAttribute("smallList",son);
        application.setAttribute("fruit",fruit);
        application.setAttribute("vegetable",vegetable);
        application.setAttribute("meat",meat);
        application.setAttribute("grain",grain);


    }
}
